<template>
	<!-- 公共的查询 -->
	<view class="">
		<!-- 主表信息 -->
		<view class="mian-table">

			<view class="mian-table-content">
				<formItem :formConfig='queryConfig' :formVal='formValueMain' @searchBaseP='searchBaseP'>
					<template #ExEnumRadio=" {config} ">
						<radio-group @change=" (val)=> radioChange(val,'Main')">
							<label class="radio">
								<radio value="1" :checked="formValueMain[config.ID] ==1  " />维修
							</label>
							<label class="radio">
								<radio value="2" :checked="formValueMain[config.ID] ==2  " />更换
							</label>
							<label class="radio">
								<radio value="3" :checked="formValueMain[config.ID] ==3  " />正常
							</label>
						</radio-group>
					</template>
				</formItem>
			</view>
		</view>


		<view class="operation">
			<button class="operation-button border-right" @click="resetForm" >
				重置
			</button>
			<button class="operation-button border-right" @click="finishSubmit" >
				立即查询
			</button>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import AllConfig from '@/common/config.js'
	import formItem from '@/components/form/form_item.vue'
	import api from '@/common/api.js'

	let _self, newConfig, activeType , initQueryJSON ;
	export default {
		data() {
			return {
				config: [],
				bstype: "",
				mainType: '',
				subType: '',
				formValueMain: '',
				formValueSub: [],
				SBData: {},
				seeType: 0,
				isDisabled: false,
				CYarray: [],
				myActiveSub: 0,
				menuCode: '',
				queryConfig: []

			}
		},
		computed: {
			...mapState(['userInfo', 'checkData']),
		},
		components: {
			formItem
		},
		onLoad(p) {
			_self = this
			newConfig = []
			initQueryJSON = {}
			this.listenOnce()
			this.menuCode = p.type
			newConfig = AllConfig.App[p.type].query
			this.queryConfig = newConfig.filter(item => {
				return item.IsShow == 1;
			})
			this.initConifg(p.type)
		},

		methods: {
			...mapMutations(["setCheckData"]),
			// 初始化配置数据
			initConifg() {
				this.formValueMain = this.initForm(newConfig)
				let queryJsonLS = getApp().globalData.queryJsonLS
				
				if(queryJsonLS){
					this.formValueMain = Object.assign(this.formValueMain, queryJsonLS)
				}
				initQueryJSON = JSON.parse(JSON.stringify(this.formValueMain))
				console.log( 'initQueryJSON' , initQueryJSON );
			},
			
			resetForm(){
				this.formValueMain =JSON.parse(JSON.stringify(initQueryJSON)) 
			},


			radioChange(e, index) {
				let v = e.detail.value
				if (index == 'Main') {
					this.formValueMain.INSPECTSTATUS = v
					this.formValueSub.forEach(i => {
						i.BILLSTATUS = v
					})
				} else {
					this.formValueSub[index].BILLSTATUS = v
				}

			},

			initForm(config) {
				let valueMain = {};
				config.forEach(item => {
					try {
						if ( item.Controls == "ExEnum") {
							let m = Array.isArray(item.Other) ?item.Other : JSON.parse(item.Other);
							valueMain[item.ID + 'Range'] = Array.isArray(m) ? m : []
							valueMain[item.ID + 'Index'] = 0
						}
					} catch (e) {
						console.error(44, e);
					}
					valueMain[item.ID] = item.Default == '0' ? 0 : (valueMain[item.Default] || '')
					if (item.Controls == "ExDateTime") {
						if (item.Other) {
							let timeVal = item.ID == 'StartTime'? this.$utils.GetDateAfter(item.Other) + " 00:00:00" : this.$utils.GetDateAfter(item.Other)
							valueMain[item.ID] = timeVal 
						}else{
							let timeVal2 = item.ID == 'EndTime'? this.$utils.GetDateAfter(0) + " 23:59:59" : this.$utils.GetDateAfter(0)
							valueMain[item.ID] = timeVal2 
						}
					}
				})
				return valueMain;
			},

			openCY(i) {
				this.myActiveSub = i
				activeSubIndex = i
				this.axios({
					url: '/API/APPCommon/GetEQMSGINFO',
					data: {},
					isLoading: 1
				}).then(res => {
					let {
						Result
					} = res;
					this.CYarray = Result
				})
			},

			saveCY(val, PK_REMARK) {
				if (!val) {
					return this.$utils.tips('请先输入巡检说明')
				}
				let URL = '/API/APPCommon/SaveEQMSG',
					data = {
						MSG: val
					};
				if (PK_REMARK) {
					URL = '/API/APPCommon/DelEQMSG'
					data = {
						BILLNO: PK_REMARK
					}
				}
				this.axios({
					url: URL,
					data: data,
					isLoading: 1
				}).then(res => {
					let {
						Result,
						Message
					} = res;
					this.$utils.tips(Message)
					if (PK_REMARK) {
						this.formValueSub[activeSubIndex].PK_REMARK = ''
					}
					this.CYarray = Result
				})
			},

			bindPickerChange(e, type) {
				let v = e.detail.value;
				if (type == 'Main') {
					this.formValueMain.VMEMO = this.CYarray[v].MSG
					this.formValueMain.PK_REMARK = this.CYarray[v].BILLNO
				} else {
					this.formValueSub[activeSubIndex].PK_REMARK = this.CYarray[v].BILLNO
					// this.$forceUpdate()
					let newItem = this.formValueSub[activeSubIndex];
					newItem.REMARK = this.CYarray[v].MSG
					this.$set(this.formValueSub, activeSubIndex, newItem)
				}
				// this.$set( this.formValueSub , activeSubIndex, {PK_REMARK: this.CYarray[v].BILLNO})
				// console.log(123,this.CYarray[v].BILLNO , this.formValueSub[activeSubIndex] );
			},

			// 基础数据查询
			searchBaseP(tnt) {
				activeType = tnt.type
				this.listenOnce()
				let queryParames = "",
					search_PK_GROUP = '';
				if(activeType == 'BD_RACK'){
					queryParames = JSON.stringify( { PK_STORE: this.formValueMain.PK_STORE })
				}else{
					queryParames = ""
				}
				let URL = "/pages/public/search?t=" + activeType + "&queryjson=" + queryParames + '&PK_GROUP=' +
					search_PK_GROUP
				uni.navigateTo({
					url: URL
				});
			},


			// 初始监听
			listenOnce() {
				uni.$off('getSelectItem')
				uni.$once("getSelectItem", function(data) {
					console.log(888, activeType , data);
					switch (activeType) {
						case "EQBOM": // 设备
							_self.formValueMain.EQUIPNAME = data.VNAME;
							_self.formValueMain.PK_EQUIP = data.BILLNO;
							break;
						case "BA_MATER": // 物资
							_self.formValueMain.MATERIALNAME = data.MATERIALNAME;
							_self.formValueMain.MATSPEC = data.MATSPEC;
							_self.formValueMain.PK_MATERIAL = data.BILLNO;
							break;
						case "BA_DEPARTMENT": // 部门
							_self.formValueMain.DEPTNAME = data.FullName;
							_self.formValueMain.PK_DEPT = data.DepartmentId;
							break;
						case "BA_ORGANIZE": // 组织
							_self.formValueMain.ORGNAME = data.FullName;
							_self.formValueMain.PK_ORG = data.OrganizeId;
							
							_self.formValueMain.SENDORGNAME = data.FullName;
							_self.formValueMain.PK_SENORG = data.OrganizeId;
							
							break;
						case "BD_SUPPLIER": // 供应商
							_self.formValueMain.SUPPLYNAME = data.VNAME;
							_self.formValueMain.PK_SUPPLY = data.BILLNO;
							
							_self.formValueMain.CUSTOMERNAME = data.VNAME;
							_self.formValueMain.PK_CUSTOMER = data.BILLNO;
							
							break;
						case "BD_STORE": // 仓库
							_self.formValueMain.STORE = data.VNAME;
							_self.formValueMain.PK_STORE = data.BILLNO;
							break;
						case "BD_RACK": // 货位
							_self.formValueMain.STATION = data.VNAME;
							_self.formValueMain.PK_STATION = data.BILLNO;
							break;
							
							
						default:
							break;
					}
				});
			},



			finishSubmit() {
				uni.$emit('reload', this.formValueMain)
				uni.navigateBack()
			},
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 110rpx;
	}

	.mr-10 {
		margin-right: 20rpx;
	}

	.mian-table {
		background-color: #ffffff;
		color: #555555;

		.main-table-title {
			margin-top: 20rpx;
			padding: 30rpx 24rpx;
			border-bottom: 1rpx solid #f1f1f1;

			.mian-table-name {
				font-weight: 800;
				font-size: 32rpx;
				position: relative;
				padding-left: 40rpx;
			}

			.mian-table-name:before {
				content: "";
				width: 10rpx;
				height: 32rpx;
				line-height: 32rpx;
				position: absolute;
				margin: 0 16rpx;
				border-radius: 10rpx;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				background-color: #1677ff;
			}
		}

		.mian-table-content {
			padding: 0 30rpx;
		}
	}

	.sub-table {
		color: #555555;

		.sub-table-title {
			background-color: #ffffff;
			display: flex;
			margin-top: 20rpx;
			padding: 30rpx 24rpx;
			border-bottom: 1rpx solid #f1f1f1;
			justify-content: space-between;
			align-items: center;

			.sub-table-title-name {
				font-weight: 800;
				font-size: 32rpx;
				position: relative;
				padding-left: 40rpx;
			}

			.sub-table-title-name:before {
				content: "";
				width: 10rpx;
				height: 32rpx;
				line-height: 32rpx;
				position: absolute;
				margin: 0 16rpx;
				border-radius: 10rpx;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				background-color: #1677ff;
			}

			.dicon {
				font-size: 50rpx;
				margin: 0 10rpx;
			}
		}

		.sub-table-content {
			margin-top: 20rpx;
			background-color: #ffffff;
			padding: 0 30rpx;

			.title {
				padding: 20rpx 0;
				border-bottom: 1rpx solid #eee;
				width: 100%;

				.name {
					font-weight: 600;
					color: #333;
					margin-bottom: 10rpx;
				}

			}

			.date {
				background-color: #f1f1f1;
				width: 250rpx;
				padding: 20rpx;
				border-radius: 10rpx;
				text-align: center;
			}

			.money {
				width: 200rpx;
			}
		}
	}

	.my-flex {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}



	.ExTextSearch-wrap {
		display: flex;
		width: 100%;
		justify-content: space-between;

		.btn-2 {
			width: 100rpx;
			height: 60rpx;
			position: relative;
			top: 0rpx;

			button {
				width: 100rpx;
				height: 60rpx;
				padding: 0;
				margin-right: 10rpx;
			}
		}

		.ExTextSearch {
			height: 120rpx;
			width: 380rpx;
		}
	}
</style>